import request, { post } from '@/utils/request';
import { ResponseBodyType } from '../typing';

export type LoginType = 'account' | 'telephone';
export type LoginStatus = 'ok' | 'error';

export interface LoginParams {
  type: LoginType;
  username: string;
  password: string;
}

export interface LoginResp {
  type: LoginType;
  success: boolean;
  token: string;
  // currentAuthority: string;
}

export interface UserInfo {
  id: string | number;
  address: string;
  avatar: string;
  country: string;
  email: string;
  group: string;
  name: string;
  phone: string;
  signature: string;
  role: {
    [key: string]: any;
  };
}

export interface CaptchaResp {
  captcha: number;
}

export interface SmsCaptchaRequest {
  mobile: string;
}

// 后端的结构体定义
export type RouteItem = {
  id: number | string;
  parentId: number | string;
  name: string;
  path: string;
  redirect: string;
  component: string;
  meta: {
    title: string | false;
    icon?: string;
    target?: '_blank' | '_self';
    hideInMenu?: boolean;
    hideChildrenInMenu?: boolean;
    authority?: string | string[];
    [key: string]: any;
  };
};

export async function postAccountLogin(params: LoginParams) {
  return request.post<LoginParams, LoginResp>('/login/account', params);
}

export async function getCurrentUser() {
  return request.get<any, UserInfo>('/currentUser');
}

export async function getCurrentUserNav() {
  return request.get<any, RouteItem[]>('/currentUserNav');
}

export async function getSmsCaptcha(params: SmsCaptchaRequest) {
  return request.get<SmsCaptchaRequest, CaptchaResp>('/message/captcha/sms', {
    params,
  });
}

// 点单登录检查token
export async function checkedCasApi(params: any) {
  return post('/cas/checked', params);
}
//修改密码
export async function initUserPassword(params: any) {
  return post('/console/user/initUserPassword', params);
}

// 获取用户信息
export async function getUserInfo() {
  return post('/loginAuth/detail', {});
}

// 获取菜单
export async function userMenu() {
  return post('/loginAuth/userMenu');
}

// 获取系统信息
export async function systemInfo() {
  return post('/system/info');
}

export type ParamType = {
  callTree: any;
  classifyTree: any;
  dealTree: any;
  xqdwTree: any;
};
// 获取全局筛选条件 【辖区单位，智能分类，警情类别，反馈类别】
export async function getParamList(params = {}) {
  return post<ResponseBodyType<ParamType>>('/common/bsparam/getParamList', params);
}

export async function accountLogin(params = {}) {
  return post('/login/login', params);
}

export async function postLogout(params = {}) {
  return post('/login/exit', params);
}
